package com.hz.framework.ajax;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.json.simple.JSONObject;
import com.hz.framework.dao.CommonDBDao;
import com.hz.framework.dao.CommonIDU;
import com.hz.framework.dao.CommonSel;
import com.hz.framework.dao.DBQ;
import com.hz.framework.util.CommonFunc;
import com.hz.supermanage.dao.HzsysAjaxDao;

/**
 * ajax DWR方法类
 * */
public class BPAjax {

	public BPAjax() {

	}

	Calendar dt = Calendar.getInstance();
	int iYear = dt.get(Calendar.YEAR);
	int iMonth = dt.get(Calendar.MONTH) + 1;
	int iDate = dt.get(Calendar.DATE);
	private List lstValue = null;
	private boolean flag = false;

	public JSONObject jsonPaging(JSONObject map) {
		return new JsonPage().jsonPaging(map);
	}
	/**
	 * @author 韩飞
	 * @time May 19, 2011	
	 * @param strPartition
	 * @return 
	 * 方法功能: //	 获取当前日期
	 */
	public String getAjaxDate(String strPartition) {
		String strTime = "";
		try {
			if (iMonth < 10) {
				if (iDate < 10) {
					strTime = iYear + strPartition + "0" + iMonth
							+ strPartition + "0" + iDate;
				} else {
					strTime = iYear + strPartition + "0" + iMonth
							+ strPartition + iDate;
				}
			} else {
				if (iDate < 10) {
					strTime = iYear + strPartition + iMonth + strPartition
							+ "0" + iDate;
				} else {
					strTime = iYear + strPartition + iMonth + strPartition
							+ iDate;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strTime;

	}
	/**
	 * 获取服务器当前时间的毫秒数
	 * @author 韩飞
	 * @time Nov 11, 2010
	 * @param inVal
	 * @return 方法功能:
	 */
	public long fromDateStringToLong(String inVal) { // 此方法计算时间毫秒
		Date date = null; // 定义时间类型
		SimpleDateFormat inputFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		try {
			date = inputFormat.parse(inVal); // 将字符型转换成日期型
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date.getTime(); // 返回毫秒数
	}
	/**
	 * @author 韩飞
	 * @time May 19, 2011	
	 * @param hql
	 * @param where
	 * @return
	 * @throws Exception 
	 * 方法功能: 通用 根据配置的hql语句查询结果 返回list结果集
	 */
	public List selListByHql(String hql, String where) throws Exception {
		hql = AjaxSQL.doAjaxSQL(hql) + where;
		CommonSel csel= new CommonSel();
		List list = csel.selHqlList(hql);
		return list;
	}
	/**
	 * @author 韩飞
	 * @time May 19, 2011	
	 * @param sql
	 * @param where
	 * @return 
	 * 方法功能: 通过sql语句 jdbc查询数据结果集
	 */
	public List selListBySql(String sql, String where) {
		sql = AjaxSQL.doAjaxSQL(sql) + where;
		CommonDBDao cdb= new CommonDBDao();
		return cdb.DBjsonList(sql);
	}
	/**
	 * @author 韩飞
	 * @time Mar 29, 2011	
	 * @param table  表名
	 * @param set  set字段信息
	 * @param where  where字段信息
	 * @return 
	 * 方法功能:  通用 根据where条件更新table表的set字段信息
	 */
	public boolean updateAnyTable(String table,String set,String where){
		String updateSql="update "+table+" set "+set+" where "+where;
		CommonDBDao cdb= new CommonDBDao();
		return cdb.insorupd(updateSql);
	}
	/**
	 * @author 韩飞
	 * @time Apr 12, 2011	
	 * @param a
	 * @param b
	 * @return 
	 * 方法功能: 获取随机编号
	 */
	public String donumber(int a, int b) {
		String number = CommonFunc.CreateRadom(a, b);
		return number;
	}
	/**
	 * @author 韩飞
	 * @time Mar 22, 2011	
	 * @param table 表名
	 * @param field 字段名
	 * @param value 对应值
	 * @return  有结果为 true  无结果为 false
	 * 方法功能: 通用 验证不重复方法
	 */
	public boolean checkOneData(String table, String field,String value) {
		boolean b = true;
		String sql = "select count("+field+") from "+table+" where "+ field +"= ? ";
		CommonSel csel= new CommonSel();
		String flag = csel.selSqlObjByST(sql,value).toString();
		if ("0".equals(flag)) {
			b = false;
		}
		return b;
	}
	/**
	 * @author 韩飞
	 * @time May 19, 2011	
	 * @param sql
	 * @param where
	 * @return 
	 * 方法功能:  通用 根据配置的sql语句 执行对数据库的操作 （插入，更新，删除）
	 */
	public boolean doExecuteSql(String sql, String where) {
		sql = AjaxSQL.doAjaxSQL(sql) + where;
		CommonDBDao cdb= new CommonDBDao();
		return cdb.insorupd(sql);
	}
	/**
	 * @author 韩飞
	 * @time May 19, 2011	
	 * @param table
	 * @param where
	 * @param value
	 * @param type
	 * @return 
	 * 方法功能: 根据单一条件是否在value集合中进行删除
	 */
	public boolean doExecuteDel(String table, String where, String value,String type) {
		if(type.equals("sql")){
			CommonDBDao cdb= new CommonDBDao();
			return cdb.DeleteSql(table,where,value);
		}else{
			CommonIDU cidu= new CommonIDU();
			return cidu.DeleteHql(table,where,value);
		}
	}
	/**
	 * @author 韩飞
	 * @time May 19, 2011	
	 * @param table
	 * @param upvalue
	 * @param upcom
	 * @param whereid
	 * @return 
	 * 方法功能: 更新表单独字段值唯一条件
	 */
	public boolean updateTable(String table, String upvalue, String where) {
		boolean isFinished = false;
		if (table == null || table == "")
			return isFinished;
		String sql = "update " + table + " set " + upvalue+ "' where " + where + "";
		CommonDBDao cdb= new CommonDBDao();
		isFinished = cdb.insorupd(sql);
		return isFinished;
	}
	
	/**
	 * @param String table 数据库表名
	 * @param String [] values where条件值
	 * @param String [] names where条件列名
	 * @param String arg0 是否自动生成SQL ""表示自动生成
	 * @param boolean flag 判断返回值为文本框,还是下拉框 true 表示文本框,false 表示下拉框
	 * @return Object
	 */
	public Object doObj(String table, String column, String[] values,
			String[] names, String arg0, boolean flag, boolean flag1)
			throws SQLException {
		StringBuilder sbSQL = new StringBuilder();
		if ("".equals(arg0)) {
			sbSQL.append("select distinct ");
			sbSQL.append(column);
			sbSQL.append(" from ");
			sbSQL.append(table);
			sbSQL.append(" where 1=1 ");
			for (int i = 0; i < names.length; i++) {
				sbSQL.append(" and ");
				sbSQL.append(names[i]);
				sbSQL.append(" =? ");
			}
		} else {
			sbSQL.append(AjaxSQL.doAjaxSQL(arg0));
			if (names.length > 0) {
				for (int i = 0; i < names.length; i++) {
					sbSQL.append(" and ");
					sbSQL.append(names[i]);
					sbSQL.append(" =? ");
				}
			}
		}
		if (flag) {
			return doSqlText(sbSQL.toString(), values);

		} else {
			return doSqlSelect(sbSQL.toString(), values, column, flag1);
		}

	}

	public JSONObject loadingJSONObject(String sql, String column)
			throws Exception {
		JSONObject jobject = new JSONObject();
		Map map = new HashMap();
		jobject.putAll(map);
		// ResultSet rs = executeQuery(sql);System.out.println(sql);
		// if (column.indexOf(",") != -1) {
		// if (rs.next()) {
		// for (int i = 0; i < column.split(",").length; i++) {
		// String strCol = column.split(",")[i];
		// jobject.put(strCol, rs.getString(strCol));
		// }
		// }
		// } else {
		// if (rs.next()) {
		// jobject.put(column, rs.getString(column));
		// }
		// }
		return jobject;
	}

	public String doSqlText(String sql, String[] values) throws SQLException {

		StringBuilder sb = new StringBuilder();
		DBQ db = new DBQ();
		try {
			ResultSet rs = db.getResultSet(sql, values);
			while (rs.next()) {
				sb.append(rs.getString(1));
			}
		} catch (SQLException e) {
			// e.printStackTrace();
			return "";
		} finally {
			db.close();
		}
		return sb.toString();
	}

	public Map doSqlSelect(String sql, String[] values, String column,
			boolean flag1) throws SQLException {

		Map<String, String> jobject = new HashMap<String, String>();
		DBQ db = new DBQ();
		ResultSet rs = db.getResultSet(sql, values);
		try {
			if (column.indexOf(",") != -1) {
				while (rs.next()) {
					for (int i = 0; i < column.split(",").length; i++) {
						String strCol = column.split(",")[i];
						jobject.put(strCol, rs.getString(strCol));
					}
				}
			} else {
				while (rs.next()) {
					if (flag1) {
						jobject.put(column, rs.getString(column));
					} else {
						jobject.put(rs.getString(column), rs.getString(column));
					}
				}
			}
		} catch (SQLException e) {
			// e.printStackTrace();
			return jobject;
		} finally {
			db.close();
		}
		return jobject;
	}
	/**
	 * @author 韩飞
	 * @time May 19, 2011	
	 * @param sql
	 * @param where
	 * @return
	 * @throws Exception 
	 * 方法功能: 给文本框赋值
	 */
	public String loadingTexts(String sql, String where) throws Exception {
		sql = AjaxSQL.doAjaxSQL(sql) + where;
		DBQ db = new DBQ();
		ResultSet rs = null;
		try {
			rs = db.getResults(sql);
			if (rs.next())
				return rs.getString(1);
			else
				return "";
		} catch (SQLException e) {
			// e.printStackTrace();
			return "";
		} finally {
			db.close();
		}
	}
	/**
	 * @author 韩飞
	 * @time May 19, 2011	
	 * @param sql
	 * @return 
	 * 方法功能: 给下拉框赋值
	 */
	public String loadingSelect(String sql) {
		sql = AjaxSQL.doAjaxSQL(sql);
		StringBuilder str = new StringBuilder();
		ResultSet rs = null;
		DBQ db = new DBQ();
		try {
			rs = db.getResults(sql);
			int count = 0;
			while (rs.next()) {
				if (count != 0)
					str.append(",");
				str.append(rs.getString(1));
				count++;
			}
		} catch (SQLException e) {
			// e.printStackTrace();
		} finally {
			db.close();
		}
		return str.toString();
	}

	public String loadingSelects(String sql, String where) {
		sql = AjaxSQL.doAjaxSQL(sql) + where;
		StringBuilder str = new StringBuilder();
		DBQ db = new DBQ();
		ResultSet rs = null;
		try {
			rs = db.getResults(sql);
			int count = 0;
			while (rs.next()) {
				if (count != 0)
					str.append(",");
				str.append(rs.getString(1));
				count++;
			}
		} catch (SQLException e) {
			// e.printStackTrace();
		} finally {
			db.close();
		}
		return str.toString();
	}

	public String loadingSelectvt(String sql, String where) {
		sql = AjaxSQL.doAjaxSQL(sql) + where;
		StringBuilder str1 = new StringBuilder();
		StringBuilder str2 = new StringBuilder();
		DBQ db = new DBQ();
		ResultSet rs = null;
		try {
			rs = db.getResults(sql);
			int count = 0;
			while (rs.next()) {
				if (count != 0) {
					str1.append(",");
					str2.append(",");
				}
				str1.append(rs.getString(1));
				str2.append(rs.getString(2));
				count++;
			}
		} catch (SQLException e) {
			// e.printStackTrace();
		} finally {
			db.close();
		}
		return str1.toString() + "<>" + str2.toString();
	}
	
	/** 组织人员角色权限管理 --------------------------------------------------------------------开始**/
	/**
	 * @author 韩飞
	 * @time Nov 11, 2010
	 * @param map  对应 值的键值对
	 * @param names
	 * @param table
	 * @return 方法功能: 保存树形结构方法
	 */
	public boolean saveForm(Map map, String names, String table,
			HttpServletRequest request) {
		HzsysAjaxDao had = new HzsysAjaxDao();
		flag = had.saveForm(map, names, table, request);
		return flag;
	}
	/**
	 * @author 韩飞
	 * @time Nov 11, 2010
	 * @param map  对应 值的键值对
	 * @param names
	 * @param table
	 * @return 方法功能: 更新树形结构信息
	 */
	public boolean updateForm(Map map, String names, String table) {
		HzsysAjaxDao had = new HzsysAjaxDao();
		flag = had.updateForm(map, names, table);
		return flag;
	}
	/**
	 * @author 韩飞
	 * @time Nov 11, 2010
	 * @param key  对应树结构id
	 * @param type  对应类别 posid 岗位, deptid 部门, orgid 单位
	 * @return 方法功能: 删除树结构，并且更新人员所属部门
	 */
	public String delTree(String key, String type) {
		HzsysAjaxDao had = new HzsysAjaxDao();
		String pid = had.delTree(key, type);
		return pid;
	}
	/**
	 * @author 韩飞
	 * @time Nov 11, 2010
	 * @param fname 文件名
	 * @param fplace  文件中间地址
	 * @param request
	 * @return 方法功能:   为点选 树结构层 读写xml文件所用
	 */
	public String XmlReadValue(String fname, String fplace,
			HttpServletRequest request) {
		HzsysAjaxDao had = new HzsysAjaxDao();
		String xmlvalue = had.XmlReadValue(fname, fplace, request);
		return xmlvalue;
	}
//	保存权限栏目表信息
	public boolean saveRole(Map map,String names,String table) {
		HzsysAjaxDao had = new HzsysAjaxDao();
		flag = had.saveRole(map, names, table);
		return flag;
	}
//	更新权限栏目表信息
	public boolean updateRole(Map map,String names,String table) {
		HzsysAjaxDao had = new HzsysAjaxDao();
		flag = had.updateRole(map, names, table);
		return flag;
	}
	//删除权限表栏目信息
	public String  deleteRole(String key,String type) {
		HzsysAjaxDao had = new HzsysAjaxDao();
		String pid = had.deleteRole(key, type);
		return pid;
	}
//	保存权限到角色  或 权限到用户
	public String SaveGramaRole(Map map,String GRflag) {
		HzsysAjaxDao had = new HzsysAjaxDao();
		String rflag = had.SaveGramaRole(map, GRflag);
		return rflag;
	}
//	删除角色信息
	public boolean deleteInfo(String table,String where ,String value) {
		HzsysAjaxDao had = new HzsysAjaxDao();
		flag = had.deleteInfo(table, where, value); //删除本节点和子节点,并删除子节点中的子节点
		return flag;
	}
//	删除客户信息及提醒信息
	public void DeleteEmpHql(String table, String where, String value){
		HzsysAjaxDao had = new HzsysAjaxDao();
		had.DeleteEmpHql(table, where, value);
	}
	/** 组织人员角色权限管理 ----------------------------------------------------------------------结束**/
	
	
	/** 工作流所用代码 -------------------------------------------开始**/
	/**
	 * @author 韩飞
	 * @time Apr 28, 2011	
	 * @param table
	 * @param com
	 * @param where
	 * @param request
	 * @return 
	 * 方法功能: 工作流中 附件类别目录，表单信息，表单继承信息，流程信息 的删除功能
	 */
//	public boolean doDelValue(String table, String com, String where, HttpServletRequest request) {
//		CommonDao cd = new CommonDao();
//		boolean vil = false;
//		String hql = "";
////		删除附件类别目录信息
//		if(table.equals("HzCommonAttachtype")){
//			hql = "delete from HzCommonAttachtype where id='" + com + "' or parentId='" + com + "'";
//			vil = cd.transactHql(hql);
////		删除流程信息
//		}else if (table.equals("HzDefFlow")) {
//			FlowDefEntity flow = new FlowDefEntity();
//			vil = flow.deleteFlowDefInfo(com , request.getRealPath("\\"));
////		删除表单信息
//		}else if (table.equals("HzDocDefBasic")) {
//			DocBasicEntity basic = new DocBasicEntity();
//			vil = basic.deleteBasicInfo(com , request.getRealPath("\\"));
//			
////		删除子流程表单继承信息
//		}else if (table.equals("HzDocRelation")) {
//			RelationEntity relation = new RelationEntity();
//			vil = relation.delByInnerTypeAndOutIDAndOutTypeSql(com, "DOCUMENT_FRAME", where,"WF_FLOW_DEF");
//		}
//		return vil;
//	}
	/**
	 * @author 韩飞
	 * @time Apr 6, 2011	
	 * @param property
	 * @return
	 * @throws SQLException 
	 * 方法功能: 输入表预览功能js查询Option
	 */
//	public String doSelectOption(String property) throws SQLException {
//		CommonSel cd =new CommonSel();
//		String opt="";
//		String hql = "from HzDefSelectvalue where selecttype='"+property+"'";
//		List list = cd.selHqlList(hql);
//		if(list!=null&&!list.isEmpty()){
//			for(int i=0;i<list.size();i++){
//				HzDefSelectvalue set = (HzDefSelectvalue)list.get(i);
//				opt+="<option value='"+set.getSelectvalue()+"'>"+set.getSelectvalue()+"</option>";
//			}
//		}
//		return opt;
//	}
	/**
	 * 
	 * @author 韩飞
	 * @time Apr 11, 2011	
	 * @param stepvalue  节点名称
	 * @param flowid  流程id
	 * @return 
	 * 方法功能: 工作流节点判断重复和进行添加
	 */
//	public boolean notNullStep(String stepvalue,String flowid){
//		CommonDao cd =new CommonDao();
//		boolean b = false;
//		List list = new ArrayList();
//		String hql = "from HzStepValue where flowid='"+flowid+"' and flowstepvalue='"+stepvalue+"'";
//		list = cd.selHqlList(hql);
//		if(list!=null&&list.size()>0) {
//			b = true;
//		}else{
//			HzStepValue inf = new HzStepValue();
//			inf.setFlowstepvalue(stepvalue);
//			inf.setFlowid(flowid);
//			inf.setAddtime(this.getAjaxDate("-"));
//			inf.setFlag("1");
//			cd.insertinfor(inf);
//		}
//		return b;
//	}
	/** 工作流所用代码 -------------------------------------------结束**/
	public static void main(String[] args) {
		String a ="-1.9";
	}
}
